//
//  ReadPageViewController.m
//  SuperNews
//
//  Created by qiang on 16/1/9.
//  Copyright © 2016年 qiang. All rights reserved.
//

#import "ReadPageViewController.h"
#import "ReadNormalTableViewCell.h"
#import "ReadThreeImageTableViewCell.h"
#import "ReadThreeImageRightBigTableViewCell.h"
#import "ReadPageModel.h"
#import <UIImageView+WebCache.h>
#import <MJRefresh.h>
#import "ReadDetailViewController.h"

@interface ReadPageViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableV;
@property (nonatomic, strong) NSMutableArray *newsArr;

@end

@implementation ReadPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.newsArr = [NSMutableArray array];
    [self getData];
    [self createViews];
}

- (void)createViews
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 10) style:UITableViewStyleGrouped];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    self.tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableV];
    
    [self.tableV registerClass:[ReadNormalTableViewCell class] forCellReuseIdentifier:@"cellNormal"];
    [self.tableV registerClass:[ReadThreeImageTableViewCell class] forCellReuseIdentifier:@"cellThree"];
    [self.tableV registerClass:[ReadThreeImageRightBigTableViewCell class] forCellReuseIdentifier:@"cellThreeBig"];
    
    self.tableV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
    }];
    self.tableV.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getDataUp)];
    self.tableV.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
    }];
    self.tableV.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getData)];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReadPageModel *readModel = [self.newsArr objectAtIndex:indexPath.section];
    if (nil == readModel.imgsrcup) {
        return 150;
    } else {
        return 300;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.newsArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReadPageModel *readModel = [self.newsArr objectAtIndex:indexPath.section];
    if (nil == readModel.imgsrcup) {
        ReadNormalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellNormal"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.imageVMain sd_setImageWithURL:(NSURL *)readModel.img placeholderImage:[UIImage imageNamed:@"placehodler"]];
        cell.labelTitle.text = readModel.title;
        cell.labelFrom.text = readModel.source;
        return cell;
    } else {
        ReadThreeImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellThree"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.imageVBig sd_setImageWithURL:(NSURL *)readModel.img placeholderImage:[UIImage imageNamed:@"placehodler"]];
        [cell.imageVUp sd_setImageWithURL:(NSURL *)readModel.imgsrcup placeholderImage:[UIImage imageNamed:@"placehodler"]];
        [cell.imageVDown sd_setImageWithURL:(NSURL *)readModel.imgsrdown placeholderImage:[UIImage imageNamed:@"placehodler"]];
        cell.labelTitle.text = readModel.title;
        cell.labelForm.text = readModel.source;
        return cell;
    }
}

- (void)getData
{
    [AFNetTool getNetWithURL:@"http://c.3g.163.com/recommend/getSubDocPic?passport=&devId=866479021081277&size=20&version=5.3.6&from=yuedu&net=wifi" body:nil headFile:nil responseStyle:YZQJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        NSMutableArray *arr = [responseObject objectForKey:@"推荐"];
        for (NSDictionary *tempDic in arr) {
            ReadPageModel *readModel = [[ReadPageModel alloc] init];
            [readModel setValuesForKeysWithDictionary:tempDic];
            NSMutableArray *arrImg = [tempDic objectForKey:@"imgnewextra"];
            if (0 != arrImg.count) {
                readModel.imgsrcup = [arrImg[0] objectForKey:@"imgsrc"];
                readModel.imgsrdown = [arrImg[1] objectForKey:@"imgsrc"];
            }
            [self.newsArr addObject:readModel];
        }
        [self.tableV reloadData];
        [self.tableV.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@", error);
    }];
}

//http://c.3g.163.com/nc/article/BD4EJFBR9001JFBS/full.html
- (void)getDataUp
{
    [AFNetTool getNetWithURL:@"http://c.3g.163.com/recommend/getSubDocPic?passport=&devId=866479021081277&size=20&version=5.3.6&from=yuedu&net=wifi" body:nil headFile:nil responseStyle:YZQJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        NSMutableArray *tmpArr = [NSMutableArray array];
        NSMutableArray *arr = [responseObject objectForKey:@"推荐"];
        for (NSDictionary *tempDic in arr) {
            ReadPageModel *readModel = [[ReadPageModel alloc] init];
            [readModel setValuesForKeysWithDictionary:tempDic];
            NSMutableArray *arrImg = [tempDic objectForKey:@"imgnewextra"];
            if (0 != arrImg.count) {
                readModel.imgsrcup = [arrImg[0] objectForKey:@"imgsrc"];
                readModel.imgsrdown = [arrImg[1] objectForKey:@"imgsrc"];
            }
            [tmpArr addObject:readModel];
        }
        [tmpArr addObjectsFromArray:self.newsArr];
        [self.newsArr removeAllObjects];
        [self.newsArr addObjectsFromArray:tmpArr];
        [self.tableV reloadData];
        [self.tableV.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@", error);
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReadDetailViewController *readDVC = [[ReadDetailViewController alloc] init];
    readDVC.mId = [[self.newsArr objectAtIndex:indexPath.section] docid];
    [self.navigationController pushViewController:readDVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
